/*******************************************************************************************************************
 * The MIT License (MIT)
 *
 * Copyright (c) 2014 Nicolas DAURES
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *******************************************************************************************************************/


/****************************************************************
 * Physic classes
 ****************************************************************/

// Manage the physic
function PhysicEngine()
{
	//=======================
	// Fields
	//=======================

    this.m_v2Gravity =                  new b2Vec2(0, -200);
    this.m_World =                      new b2World(this.m_v2Gravity, true);
    this.m_fTimeStep =                  1.0 / FPS;
    this.m_iVelocityIterationCount =    10;
    this.m_iPositionIterationCount =    10;
}


//=======================
// Accessors
//=======================

PhysicEngine.prototype.getWorld = function()
{
    return this.m_World;
}

PhysicEngine.prototype.getGravity = function()
{
    return this.m_v2Gravity;
}

PhysicEngine.prototype.setGravity = function(a_v2Gravity)
{
    this.m_v2Gravity = a_v2Gravity;
}

PhysicEngine.prototype.getVelocityIterationCount = function()
{
    return this.m_iVelocityIterationCount;
}

PhysicEngine.prototype.setVelocityIterationCount = function(a_iVelocityIterationCount)
{
    this.m_iVelocityIterationCount = a_iVelocityIterationCount;
}

PhysicEngine.prototype.getPositionIterationCount = function()
{
    return this.m_iPositionIterationCount;
}

PhysicEngine.prototype.setPositionIterationCount = function(a_iPositionIterationCount)
{
    this.m_iPositionIterationCount = a_iPositionIterationCount;
}


//=======================
// Operations
//=======================

PhysicEngine.prototype.update = function()
{
    this.m_World.Step(this.m_fTimeStep, this.m_iVelocityIterationCount, this.m_iPositionIterationCount);
}

PhysicEngine.m_Instance = null;
PhysicEngine.getInstance = function()
{  
    if (this.m_Instance == null)
    {  
		this.m_Instance = new PhysicEngine();  
    }  

    return this.m_Instance;  
}
